package com.xxq.semester.controller.chat;


import com.xxq.semester.entity.chat.ChatSessions;
import com.xxq.semester.entity.utils.ResponseEntity;
import com.xxq.semester.service.impl.chat.ChatSessionsServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author saki
 * @since 2025-07-13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/chat-sessions")
@Tag(name="聊天会话管理",description = "增删查")
public class ChatSessionsController {
    private final ChatSessionsServiceImpl chatSessionsService;
    @GetMapping("/{userId}")
    @Operation(summary = "获取聊天会话", description = "根据用户ID获取该用户的所有聊天会话")
    public ResponseEntity<List<ChatSessions>> getChatSessionsByUserId(@PathVariable Integer userId){
        List<ChatSessions> chatSessions=chatSessionsService.getByUserId(userId);
        if(chatSessions==null || chatSessions.isEmpty()){
            return ResponseEntity.error("查询失败");
        }
        return ResponseEntity.success(chatSessions);
    }
    @PostMapping("/addChatSession")
    @Operation(summary = "添加聊天会话", description = "添加聊天会话")
    public ResponseEntity<ChatSessions> addChatSession(@RequestBody ChatSessions chatSessions){
        chatSessions.setCreatedAt(LocalDateTime.now());
        boolean result=chatSessionsService.addChatSession(chatSessions);
        if(!result){
            return ResponseEntity.error("添加失败");
        }
        return ResponseEntity.success("添加成功",chatSessions);
    }
    @DeleteMapping("/{userId}")
    @Operation(summary = "删除聊天会话", description = "删除该会话下所有聊天记录")
    public ResponseEntity<String> deleteChatSessionByUserId(@PathVariable Integer userId){
        boolean result=chatSessionsService.deleteByUserId(userId);
        if(!result){
            return ResponseEntity.error("删除失败");
        }
        return ResponseEntity.success("删除成功");
    }
}
